<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2015-2020.
// +----------------------------------------------------------------------
// | Describe: 支付结果处理类
// +----------------------------------------------------------------------
// | Author: woniu
// +----------------------------------------------------------------------

class payResult{
	/**
	* 异步支付结果回调 ，处理业务逻辑
	*
	* @access public
	* @name
	* @param mixed  参数一的说明
	* @return array
	*/
	static function payNotify($params){
		global $_W;
		if($params['result'] == 'success') {
			if (empty($params['type'])){
				$paylogs=pdo_get('core_paylog', array('tid' => $params['tid']));
				$params['uniacid']=$paylogs['uniacid'];
				$params['type']=$paylogs['type'];
				$params['uniontid']=$paylogs['uniontid'];
				$params['fee']=$paylogs['fee'];
			}
            $records = pdo_get('aide_activity_records', array('orderno' => $params['tid'], 'uniacid' => $params['uniacid']));
			$activity = model_activity::getSingleActivity($records['activityid'], '*');
			$_W['oauth']['uniacid'] = $params['user'] != $records['openid'] ? '':$_W['uniacid'];
			$_W['uniacid'] = $params['uniacid'];

			if(!empty($records)) {
				$data = array(
					'paytype' => $params['type'],
					'status' => 1,
					'paytime' => date('Y-m-d H:i:s',TIMESTAMP),
					'payprice' => $params['fee']
				);
				$fee = $params['fee'];
				if ($params['from'] == 'notify'){
					//Array ( [transaction_id] => 4001602001201611230627952393) [is_usecard] => 0 [card_type] => 0 [card_fee] => 0.10 [card_id] => 0)
					if(!empty($params['tag'])) {
						$params['tag'] = iunserializer($params['tag']);
						$data['uniontid'] = $params['uniontid'];
						$data['transid'] = $params['tag']['transaction_id'];
					}else{
						aide_message ('抱歉，请正确支付您的订单！', '', 'error');
					}
				}

				if ($records['status'] == 0) {
					//$listNum = pdo_fetchall("SELECT hexiaoma as num FROM " . tablename ('aide_activity_records') . " WHERE activityid = ".$activity['id']);
					//$data['hexiaoma'] = createNumber($listNum);
					$data['hexiaoma'] = createRandomNumber(8);

					if(!empty($activity['merchantid'])){
						model_merchant::updateAmount($fee, $activity['merchantid'],$records['id'],1,'订单支付成功');  //主办方进账
						pdo_insert("aide_merchant_money_record",array('merchantid'=>$activity['merchantid'],'uniacid'=>$_W['uniacid'],'money'=>$records['price'],'recordsid'=>$records['id'],'createtime'=>TIMESTAMP,'type'=>1,'detail'=>'支付成功<br>订单号：' . $records['orderno']));
					}
					$pay_result = pdo_update('aide_activity_records', $data, array('orderno' => $params['tid'], 'uniacid' => $params['uniacid']));

					//生成二维码
					createQrcode::creategroupQrcode($params['tid']);
					if ($_W['_config']['mmsg']){//管理通知
						if ($activity['merchantid'])
						$merchant = model_merchant::getSingleMerchant($activity['merchantid'], '*');//读取主办方
						$openids = unserialize($activity['openids']);
						$openids = !empty($openids) ? $openids : unserialize($merchant['messageopenid']);
						$openids = !empty($openids) ? $openids : $_W['_config']['openids'];
						if (!empty($openids)){
							foreach($openids as $key=> $value){
								message::admin_notice($value, $activity, $records['id'], '');
							}
						}
					}

					//积分变更
					if ($_W['_config']['creditstatus'] == 1){
						aide_load()->model('credit');
						$activity['prize'] = unserialize($activity['prize']);
						$orderMarket = array();
						if(!empty($records['marketing'])) $orderMarket = unserialize($records['marketing']);
						if($activity['prize']['credit'] > 0){
							$credit = $orderMarket['credit_double'] ? $activity['prize']['credit'] * 2 : $activity['prize']['credit'];
							$credit_result = credit_update_credit1($records['uid'], $credit, "报名：增加" . $credit . '积分', $activity['merchantid']);
						}
						if($orderMarket['deduct']){
							$deduct = $orderMarket['deduct'];
							if($deduct[0]) credit_update_credit1($records['uid'], -1 * $deduct[0], "积分抵扣：减少" . $deduct[0] . '积分', $activity['merchantid']);
						}
						if($activity['costcredit']){
							credit_update_credit1($records['uid'], -1 * $activity['costcredit'], "参与活动消耗：减少" .$activity['costcredit'] . '积分', $activity['merchantid']);
						}
					}
				}
				header("location:".app_url('home/index/success', array('recordid' => $records['id'],'openid'=>$records['openid'])));
            }
		}
	}
	/**
	* 函数的含义说明
	*
	* @access public
	* @name 方法名称
	* @param mixed  参数一的说明
	* @return array
	*/
	static function payReturn($params){
		global $_W;

	}
	/**
	* 函数的含义说明
	*
	* @access public
	* @name 方法名称
	* @param mixed  参数一的说明
	* @return array
	*/
	static function getPayData($params,$order_out,$goodsInfo){
	 	global $_W;

	}
}



?>
